home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.text.BreakIterator;
-
- public class Utilities {
- public static final int drawTabbedText(Segment s, int x, int y, Graphics g, TabExpander e, int startOffset) {
- FontMetrics metrics = g.getFontMetrics();
- int nextX = x;
- char[] txt = s.array;
- int flushLen = 0;
- int flushIndex = s.offset;
- int n = s.offset + s.count;
-
- for(int i = s.offset; i < n; ++i) {
- if (txt[i] == '\t') {
- if (flushLen > 0) {
- g.drawChars(txt, flushIndex, flushLen, x, y);
- flushLen = 0;
- }
-
- flushIndex = i + 1;
- if (e != null) {
- nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
- } else {
- nextX += metrics.charWidth(' ');
- }
-
- x = nextX;
- } else if (txt[i] != '\n' && txt[i] != '\r') {
- ++flushLen;
- nextX += metrics.charWidth(txt[i]);
- } else {
- if (flushLen > 0) {
- g.drawChars(txt, flushIndex, flushLen, x, y);
- flushLen = 0;
- }
-
- flushIndex = i + 1;
- x = nextX;
- }
- }
-
- if (flushLen > 0) {
- g.drawChars(txt, flushIndex, flushLen, x, y);
- }
-
- return nextX;
- }
-
- public static final int getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset) {
- int index = getTabbedTextOffset(s, metrics, x0, x, e, startOffset);
-
- for(int i = s.offset + Math.min(index, s.count - 1); i >= s.offset; --i) {
- char ch = s.array[i];
- if (Character.isWhitespace(ch)) {
- index = i - s.offset + 1;
- break;
- }
- }
-
- return index;
- }
-
- public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException {
- Document doc = c.getDocument();
- Element line = getParagraphElement(c, offs);
- int lineStart = line.getStartOffset();
- int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
- String s = doc.getText(lineStart, lineEnd - lineStart);
- if (s != null && s.length() > 0) {
- BreakIterator words = BreakIterator.getWordInstance();
- words.setText(s);
- int wordPosition = offs - lineStart;
- if (wordPosition >= words.last()) {
- wordPosition = words.last() - 1;
- }
-
- words.following(wordPosition);
- offs = lineStart + words.next();
- }
-
- return offs;
- }
-
- public static final Element getParagraphElement(JTextComponent c, int offs) {
- Document doc = c.getDocument();
- if (doc instanceof StyledDocument) {
- return ((StyledDocument)doc).getParagraphElement(offs);
- } else {
- Element map = doc.getDefaultRootElement();
- int index = map.getElementIndex(offs);
- Element paragraph = map.getElement(index);
- return paragraph;
- }
- }
-
- public static final int getPositionAbove(JTextComponent c, int offs, int x) throws BadLocationException {
- int lastOffs = getRowStart(c, offs) - 1;
- int bestSpan = 32767;
- int y = 0;
- Rectangle r = null;
- if (lastOffs >= 0) {
- r = c.modelToView(lastOffs);
- y = r.y;
- }
-
- while(r != null && y == r.y) {
- int span = Math.abs(r.x - x);
- if (span < bestSpan) {
- offs = lastOffs;
- bestSpan = span;
- }
-
- --lastOffs;
- r = lastOffs >= 0 ? c.modelToView(lastOffs) : null;
- }
-
- return offs;
- }
-
- public static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
- int lastOffs = getRowEnd(c, offs) + 1;
- int bestSpan = 32767;
- int n = c.getDocument().getLength();
- int y = 0;
- Rectangle r = null;
- if (lastOffs <= n) {
- r = c.modelToView(lastOffs);
- y = r.y;
- }
-
- while(r != null && y == r.y) {
- int span = Math.abs(x - r.x);
- if (span < bestSpan) {
- offs = lastOffs;
- bestSpan = span;
- }
-
- ++lastOffs;
- r = lastOffs <= n ? c.modelToView(lastOffs) : null;
- }
-
- return offs;
- }
-
- public static final int getPreviousWord(JTextComponent c, int offs) throws BadLocationException {
- Document doc = c.getDocument();
- Element line = getParagraphElement(c, offs);
- int lineStart = line.getStartOffset();
- int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
- String s = doc.getText(lineStart, lineEnd - lineStart);
- if (s != null && s.length() > 0) {
- BreakIterator words = BreakIterator.getWordInstance();
- words.setText(s);
- int wordPosition = offs - lineStart;
- if (wordPosition >= words.last()) {
- wordPosition = words.last() - 1;
- }
-
- words.following(wordPosition);
- if (offs == lineStart + words.previous()) {
- words.previous();
- int o = words.previous();
- if (o == -1) {
- offs = lineStart;
- } else {
- offs = lineStart + o;
- }
- }
- }
-
- return offs;
- }
-
- public static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException {
- Rectangle r = c.modelToView(offs);
- int n = c.getDocument().getLength();
- int lastOffs = offs;
-
- for(int y = r.y; r != null && y == r.y; r = lastOffs <= n ? c.modelToView(lastOffs) : null) {
- offs = lastOffs++;
- }
-
- return offs;
- }
-
- public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
- Rectangle r = c.modelToView(offs);
- int lastOffs = offs;
-
- for(int y = r.y; r != null && y == r.y; r = lastOffs >= 0 ? c.modelToView(lastOffs) : null) {
- offs = lastOffs--;
- }
-
- return offs;
- }
-
- public static final int getTabbedTextOffset(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset) {
- int currX = x0;
- int nextX = x0;
- char[] txt = s.array;
- int n = s.offset + s.count;
-
- for(int i = s.offset; i < n; ++i) {
- if (txt[i] == '\t') {
- if (e != null) {
- nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
- } else {
- nextX += metrics.charWidth(' ');
- }
- } else {
- nextX += metrics.charWidth(txt[i]);
- }
-
- if (x >= currX && x < nextX) {
- if (x - currX < nextX - x) {
- return i - s.offset;
- }
-
- return i + 1 - s.offset;
- }
-
- currX = nextX;
- }
-
- return s.count;
- }
-
- public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x, TabExpander e, int startOffset) {
- int nextX = x;
- char[] txt = s.array;
- int n = s.offset + s.count;
-
- for(int i = s.offset; i < n; ++i) {
- if (txt[i] == '\t') {
- if (e != null) {
- nextX = (int)e.nextTabStop((float)nextX, startOffset + i - s.offset);
- } else {
- nextX += metrics.charWidth(' ');
- }
- } else {
- nextX += metrics.charWidth(txt[i]);
- }
- }
-
- return nextX - x;
- }
-
- public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
- Document doc = c.getDocument();
- Element line = getParagraphElement(c, offs);
- int lineStart = line.getStartOffset();
- int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
- String s = doc.getText(lineStart, lineEnd - lineStart);
- if (s != null && s.length() > 0) {
- BreakIterator words = BreakIterator.getWordInstance();
- words.setText(s);
- int wordPosition = offs - lineStart;
- if (wordPosition >= words.last()) {
- wordPosition = words.last() - 1;
- }
-
- offs = lineStart + words.following(wordPosition);
- }
-
- return offs;
- }
-
- public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException {
- Document doc = c.getDocument();
- Element line = getParagraphElement(c, offs);
- int lineStart = line.getStartOffset();
- int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
- String s = doc.getText(lineStart, lineEnd - lineStart);
- if (s != null && s.length() > 0) {
- BreakIterator words = BreakIterator.getWordInstance();
- words.setText(s);
- int wordPosition = offs - lineStart;
- if (wordPosition >= words.last()) {
- wordPosition = words.last() - 1;
- }
-
- words.following(wordPosition);
- offs = lineStart + words.previous();
- }
-
- return offs;
- }
- }
-